<?php defined('_JEXEC') or die('Restricted access'); ?>
<?

	JRequest::setVar( 'hidemainmenu', 1 );	
	//setup apikey googlemap
	JHTML::script('svmap.js','administrator/components/com_nv/',false);
	JHTML::script('maps?file=api&amp;v=2&amp;key='.$this->config_params->get('gmapkey'),'http://maps.google.com/',false);
	JHTML::script('api?file=uds.js&v=1.0&key='.$this->config_params->get('gmapkey'),'http://www.google.com/uds/',false);
	JHTML::script('gmlocalsearch.js','http://www.google.com/uds/solutions/localsearch/',false);
	JHTML::stylesheet('gsearch.css','http://www.google.com/uds/css/');
	JHTML::stylesheet('gmlocalsearch.css','http://www.google.com/uds/solutions/localsearch/');
	
	$marker = 'http://www.google.com/mapfiles/marker.png';
?>
	<style type="text/css">
		.gsc-resultsRoot, .gsc-branding, .gsc-branding {
			display:none;
			visibility:hidden;
		}
	</style>
	<script type="text/javascript">

		var app;
		function OnLoad() {
			app = new App();
		}
		
		function App() {
		
			this.myMap = null;
			
			this.myMap = new GMap2(document.getElementById("svmap_canvas"),{mapTypes:[G_NORMAL_MAP]});
			
			this.myMap.addControl(new GLargeMapControl());
			
			<?php
				if(!empty($this->objek->latitude) && !empty($this->objek->longitude)) {
					echo 'var point = new GLatLng('.$this->objek->latitude.', '.$this->objek->longitude.');';
					echo 'this.myMap.setCenter(new GLatLng('.$this->objek->latitude.', '.$this->objek->longitude.'), '.$this->config_params->get('secondzoom').');';
				} else {
					echo 'var point = new GLatLng('.$this->config_params->get('center').');';
					echo 'this.myMap.setCenter(new GLatLng('.$this->config_params->get('center').'), '.$this->config_params->get('secondzoom').');';
				}
				
			?>
			
			var image = new Image();
			

			image.src ='<?php echo $marker ?>' ;

			var myIcon = new GIcon();
			myIcon.iconAnchor = new GPoint((image.width/2), image.height);
			myIcon.image = image.src;
			
			var markerOptions = { icon:myIcon, draggable: true, "title":"<?php echo $this->objek->name; ?>" };
			
			var gmarker = new GMarker(point, markerOptions);
				
															
			this.myMap.addOverlay(gmarker);		
			
			GEvent.addListener(gmarker, "dragend", function() {
				//document.getElementById("lat").value = this.getLatLng().lat();
				//document.getElementById("lon").value = this.getLatLng().lng();
				document.getElementById("latitude").value = this.getLatLng().lat();
				document.getElementById("longitude").value = this.getLatLng().lng();				
			});
			
			GEvent.addListener(this.myMap, "singlerightclick", function(point) {
				point = this.fromContainerPixelToLatLng(point);
				gmarker.setLatLng(point);
				//document.getElementById("lat").value = gmarker.getLatLng().lat();
				//document.getElementById("lon").value = gmarker.getLatLng().lng();
				document.getElementById("latitude").value = gmarker.getLatLng().lat();
				document.getElementById("longitude").value = gmarker.getLatLng().lng();				
			});				
			
			var searchControl = new GSearchControl();
			
			var localSearch = new GlocalSearch();
			
			var options = new GsearcherOptions();
			options.setExpandMode(GSearchControl.EXPAND_MODE_CLOSED);
			searchControl.addSearcher(new GlocalSearch(), options);
			
			localSearch.setCenterPoint(this.myMap);
			
			searchControl.draw(document.getElementById("searchcontrol"));
			
			searchControl.setSearchCompleteCallback(this, App.prototype.OnSearchComplete);
			searchControl.setSearchStartingCallback(this, App.prototype.OnSearchStarting);
			
			refreshSearch();
			
			<?php
				if (empty($this->objek->latitude) && empty($this->objek->longitude)) {
					echo 'searchControl.execute();';
				}
			?>
		}
		
		App.prototype.OnSearchComplete = function(sc, searcher) {
			if ( searcher.results && searcher.results.length > 0) {
				this.myMap.clearOverlays();
				for (var i = 0; i < searcher.results.length; i++) {
					var result = searcher.results[i];
					
					if (result.GsearchResultClass == GlocalSearch.RESULT_CLASS ) {
						var markerObject = new Object();
						markerObject.result = result;
						markerObject.latLng = new GLatLng(parseFloat(result.lat), parseFloat(result.lng));

						var image = new Image();
						
						for (i=0; i<document.forms[0].marker.length; i++) {
							if (document.forms[0].marker[i].checked) {
								image.src = document.forms[0].marker[i].value;
							}
						}

						var myIcon = new GIcon();
						myIcon.iconAnchor = new GPoint((image.width/2), image.height);
						myIcon.image = image.src;
						
						markerObject.markerOptions = { icon:myIcon, draggable: true, "title":"<?php echo $this->objek->name; ?>" };
						
						markerObject.gmarker = new GMarker(markerObject.latLng, markerObject.markerOptions);
						var clickHandler = method_closure(this, App.prototype.OnMarkerClick, [markerObject]);
						GEvent.bind(markerObject.gmarker, "click", this, clickHandler);

						this.myMap.addOverlay(markerObject.gmarker);
						result.__markerObject__ = markerObject;
						
						this.myMap.setCenter(markerObject.latLng);
						
						//document.getElementById("lat").value = markerObject.latLng.lat();
						//document.getElementById("lon").value = markerObject.latLng.lng();
						document.getElementById("latitude").value = markerObject.latLng.lat();
						document.getElementById("longitude").value = markerObject.latLng.lng();						
						
						GEvent.addListener(markerObject.gmarker, "dragend", function() {
							//document.getElementById("lat").value = this.getLatLng().lat();
							//document.getElementById("lon").value = this.getLatLng().lng();
							document.getElementById("latitude").value = this.getLatLng().lat();
							document.getElementById("longitude").value = this.getLatLng().lng();							
						});
						
						GEvent.addListener(this.myMap, "singlerightclick", function(point) {
							point = this.fromContainerPixelToLatLng(point);
							markerObject.gmarker.setLatLng(point);
							//document.getElementById("lat").value = markerObject.gmarker.getLatLng().lat();
							//document.getElementById("lon").value = markerObject.gmarker.getLatLng().lng();
							document.getElementById("latitude").value = markerObject.gmarker.getLatLng().lat();
							document.getElementById("longitude").value = markerObject.gmarker.getLatLng().lng();							
						});
					}
				}
			}
		}
		
		App.prototype.OnSearchStarting = function(sc, searcher, query) {
			//
		}			
		function method_closure(object, method, opt_argArray) {
			return function() {
				return method.apply(object, opt_argArray);
			}
		}		
		GSearch.setOnLoadCallback(OnLoad);			


	</script>	
	<script language="javascript" type="text/javascript">
	function submitbutton(pressbutton) {
		var form = document.adminForm;
		if (pressbutton == 'cancel') {
			submitform( pressbutton );
			return;
		}
		// do field validation
		if ( form.name.value == "" ) {
			alert( "<?php echo JText::_( 'Harap memasukkan Nama Objek Pariwisata ', true ); ?>" );
		} 
		else if ( form.latitude.value == "") {
			alert( "<?php echo JText::_( 'Harap memasukkan posisi latitude ', true ); ?>" );
		}
		else if ( form.longitude.value == "") {
			alert( "<?php echo JText::_( 'Harap memasukkan posisi longitude ', true ); ?>" );
		}
		else if ( form.propinsi_id.value == '-1') {
			alert( "<?php echo JText::_( 'Harap mengisi lokasi propinsi pariwisata ', true ); ?>" );
		}		
		else if ( form.kota_id.value == '-1') {
			alert( "<?php echo JText::_( 'Harap mengisi lokasi kota pariwisata ', true ); ?>" );
		}		
		else if ( form.id_bandara.value == '-1') {
			alert( "<?php echo JText::_( 'Harap memasukkan bandara terdekat ', true ); ?>" );
		}		
		else {
			submitform( pressbutton );
		}
	}
	//-->
	</script>
	
<form action="index.php" method="post" name="adminForm" id="adminForm">
<div class="col100">		<fieldset class="adminform">
			<legend>Memasukkan Data Pariwisata</legend>
        <table class="admintable">
		<tr>
          <td width="100" align="right" class="key">
          Nama Tempat
          </td>
          <td>
            <input class="text_area" type="text" name="name" 
               id="name" size="50" maxlength="250" 
               value="<?php echo $this->objek->name;?>" />
          </td>
        </tr>
			<script language="javascript" type="text/javascript">
			
			var sectioncategories = new Array;
			<?php
			$i = 0;
			foreach ($this->sectioncategories as $k=>$items) {
				foreach ($items as $v) {
					echo "sectioncategories[".$i++."] = new Array( '$k','".addslashes( $v->kota_id )."','".addslashes( $v->nama_kota )."' );\n\t\t";
				}
			}
			?>

			var sectioncategories2 = new Array;
			<?php
			
			$i = 0;
			foreach ($this->sectioncategories2 as $k=>$items) {
				foreach ($items as $v) {
					echo "sectioncategories2[".$i++."] = new Array( '$k','".addslashes( $v->id_bandara )."','".addslashes( $v->nama_bandara )."' );\n\t\t";
				}
			}
			
			?>			
			</script>
        <tr>
          <td width="100" align="right" class="key">
            Propinsi:
          </td>
          <td>			
			<?php echo $this->lists['propinsi_id']; ?>
          </td>
        </tr>
        <tr>
          <td width="100" align="right" class="key">
            Kota Pariwisata:
          </td>
          <td>
			<?php echo $this->lists['kota_id']; ?>
          </td>
        </tr>		
        <tr>
          <td width="100" align="right" class="key">
            Bandara Terdekat:
          </td>
          <td>           
			<?php echo $this->lists['id_bandara']; ?>
          </td>
        </tr>
		<tr>
          <td width="100" align="right" class="key">
            Keterangan:
          </td>
          <td>
            <textarea class="text_area" cols="20" rows="4" 
               name="keterangan" id="notes" style="width:500px"><?php echo 
               $this->objek->keterangan; ?></textarea>
          </td>
        </tr>	

        <tr>
		<td width="100" align="right" class="key">
            Latitude & Longitude:
          </td>
          <td>
            <input class="text_area" type="text" name="latitude" 
               id="latitude" size="50" maxlength="250" 
               value="<?php echo $this->objek->latitude;;?>" />
            <input class="text_area" type="text" name="longitude" 
               id="longitude" size="50" maxlength="250" 
               value="<?php echo $this->objek->longitude;?>" />			   
          </td>
        </tr>	
		<tr>
			<td width="100" align="right" class="key">
				<span class="editlinktip hasTip" title="LOKASI PETA">
					<label for="marker"><?php echo JText::_( 'POSISI' ); ?></label>
					<?php //echo JHTML::_('image', 'administrator/templates/khepri/images/menu/icon-16-info.png', JText::_('NOTES') ); ?>
				</span>
			</td>
			<td>
				<div id="searchcontrol"></div>
				<div id="svmap_canvas" style="width: 1000px; height: 500px; overflow:hidden;"></div>
			</td>
		</tr>		
        </table>
		</fieldset></div>
<div class="clr"></div>

<input type="hidden" name="option" value="com_nv" />
<input type="hidden" name="id_pariwisata" value="<?php echo $this->objek->id_pariwisata; ?>" />
<input type="hidden" name="task" value="" />
<input type="hidden" name="controller" value="objek" />

</form>
